package com.rightsidetech.http.controller.agentAdmin.home;

import com.rightsidetech.commom.base.MonthQueryRequest;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.dto.response.AgentHomeTopResp;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.http.aspect.Auth;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.system.entity.store.SysStoreInfo;
import com.rightsidetech.system.entity.store.SysStoreLoginInfo;
import com.rightsidetech.system.service.SysAgentInfoService;
import com.rightsidetech.system.service.SysStoreInfoService;
import com.rightsidetech.system.service.SysStoreLoginInfoService;
import com.rightsidetech.trade.dtos.response.HomeMonthSaleResp;
import com.rightsidetech.trade.dtos.response.HomeSpscSaleResp;
import com.rightsidetech.trade.dtos.response.HomeStoreSaleResp;
import com.rightsidetech.trade.service.TdCommodityUserOrderService;
import com.rightsidetech.user.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import java.util.List;

/**
 * @Package: com.rightsidetech.http.controller.agentAdmin.home
 * @ClassName: AgentHomeController
 * @Author: yinj
 * @CreateTime: 2021/12/28 11:15
 * @Description: 代理商平台首页
 */
@Api(tags = "代理商平台首页")
@RestController
@RequestMapping("/agentAdmin/home")
@ShenyuSpringMvcClient(path = "/agentAdmin/home/**")
@Slf4j
public class AgentHomeController extends BaseController {

    @Resource
    private TdCommodityUserOrderService tdCommodityUserOrderService;
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private SysAgentInfoService sysAgentInfoService;
    @Resource
    private SysStoreInfoService sysStoreInfoService;


    @Log
    @Token
    @Auth(bizType = Auth.AGENT)
    @ApiOperation("顶部数据")
    @GetMapping("/top")
    public ResponseData<AgentHomeTopResp> top(){

        //获取登录账号代理商id
        Long agentInfoId = sysAgentInfoService.findAgentInfoId(Long.parseLong(getAttribute()));
        if (StringUtils.isEmpty(agentInfoId)){
            return ResponseData.fail(CodeIdEnum.Expired);
        }
        //订单统计
        AgentHomeTopResp agentHomeTopResp = tdCommodityUserOrderService.orderStatisByAgentId(agentInfoId);

        //门店
        AgentHomeTopResp storeTopResp = sysAgentInfoService.sysStoreStatisByAgentId(agentInfoId);

        agentHomeTopResp.setTotalStore(storeTopResp.getTotalStore());
        agentHomeTopResp.setWarnStore(storeTopResp.getWarnStore());
        return ResponseData.success(agentHomeTopResp);
    }


    @Log
    @Token
    @Auth(bizType = Auth.AGENT)
    @ApiOperation("当日电池销售按型号统计")
    @GetMapping("/today_specSale")
    public ResponseData<List<HomeSpscSaleResp>> todaySpecSale(){

        //获取登录账号代理商id
        Long agentInfoId = sysAgentInfoService.findAgentInfoId(Long.parseLong(getAttribute()));
        if (StringUtils.isEmpty(agentInfoId)){
            return ResponseData.fail(CodeIdEnum.Expired);
        }

        List<HomeSpscSaleResp> list = tdCommodityUserOrderService.todaySpecSale(null, agentInfoId);
        return ResponseData.success(list);
    }

    @Token
    @Auth(bizType = Auth.AGENT)
    @ApiOperation("当日电池销售门店排名top5")
    @GetMapping("/today_storeSale")
    public ResponseData<List<HomeStoreSaleResp>> todayStoreSale(){

        //获取登录账号代理商id
        Long agentInfoId = sysAgentInfoService.findAgentInfoId(Long.parseLong(getAttribute()));
        if (StringUtils.isEmpty(agentInfoId)){
            return ResponseData.fail(CodeIdEnum.Expired);
        }

        List<HomeStoreSaleResp> list = tdCommodityUserOrderService.todayStoreSale(agentInfoId);
        if (!CollectionUtils.isEmpty(list)){
            for (HomeStoreSaleResp resp : list){
                SysStoreInfo sysStoreInfo = sysStoreInfoService.selectByPrimaryKey(resp.getStoreId());
                if (!ObjectUtils.isEmpty(sysStoreInfo)){
                    resp.setStoreName(sysStoreInfo.getStoreName());
                }
            }
        }

        return ResponseData.success(list);
    }



    @Log
    @Token
    @Auth(bizType = Auth.AGENT)
    @ApiOperation("电池销售按月统计")
    @GetMapping("/month_sale")
    public ResponseData<List<HomeMonthSaleResp>> monthSale(MonthQueryRequest request){
        ConstraintViolation validate = BeanValidator.validate(request);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }

        //获取登录账号代理商id
        Long agentInfoId = sysAgentInfoService.findAgentInfoId(Long.parseLong(getAttribute()));
        if (StringUtils.isEmpty(agentInfoId)){
            return ResponseData.fail(CodeIdEnum.Expired);
        }

        return tdCommodityUserOrderService.monthSale(request,null,agentInfoId);

    }

}
